import axios from "./index";
import { getNow, getComeSon, getCity, getDetail, getCinema, getInfo, getFilms, resLogin, resRegister ,getCinemaFilm} from "./api"

export function getNowPlays(pageNum) {
  return axios(getNow + '?pageNum=' + pageNum)
}
export function getComing(pageNum) {
  return axios(getComeSon + '?pageNum=' + pageNum)
}

// 获取城市 按照字母排序
export function getsCity() {
  return axios(getCity).then(res => {
    let cities = res.data.cities
    // console.log(cities)
    let city = []
    let zimu = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

    for (let i = 0; i < zimu.length; i++) {
      let find = []
      find = cities.filter(item => item.pinyin.slice(0, 1) === zimu[i])

      let obj = zimu[i].toUpperCase()
      city.push({ find, key: obj })
    }
    city = city.filter(item => item.find.length !== 0)

    return new Promise(resolve => resolve(city))
  })
}
// 获取热门城市
export function getHotCity() {
  return axios(getCity).then(res => {
    let cities = res.data.cities
    let hotCity = cities.filter(item => item.isHot === 1)

    return new Promise(resolve => resolve(hotCity))
  })
}

// 获取城市影院
export function getCityCinemas(id, pageNum) {
  return axios(getCinema + '?cityId=' + id + '&pageNum=' + pageNum + '&pageSize=10')
}

// 获取城市中的所有区
export function getCityAreas(id) {
  // return axios(getCinema+'?cityId='+id)
  return axios(getCinema + '?cityId=' + id + '&pageNum=1&pageSize=1').then(res => {
    let total = res.data.total
    return new Promise(resolve => resolve(total))
  })
}

// 获取城市的所有区
export function getCityById(id, total) {
  return axios(getCinema + '?cityId=' + id + '&pageNum=1&pageSize=' + total).then(item => {
    let arr = []
    item.data.cinemas.forEach(res => {
      arr.push(res.districtName)
    })
    let area = [...new Set(arr)]
    let area1 = []
    area.forEach((item, index) => {
      area1.push({ text: item, value: index + 1 })
    })
    return new Promise(resolve => resolve(area1))
  })
}

// 获取对应区的影院
export function getAreaCinema(id, total, area) {
  let arr = []
  return axios(getCinema + '?cityId=' + id + '&pageNum=1&pageSize=' + total).then(item => {
    item.data.cinemas.forEach(item => {
      if (item.districtName === area) {
        arr.push(item)
      }
    })
    return new Promise(resolve => resolve(arr))
  })
}

// 获取影院信息
export function getDistrict(id) {
  return axios(getInfo + '?cinemaId=' + id)
}

// 影院排片
export function getFilmsInfo(id) {
  return axios(getFilms + '?cinemaId=' + id)
}

// 电影详情
export function getDetailInfo(id) {
  return axios(getDetail + '?filmId=' + id)
}

// 影院排片
export function getCinArr(id) {
  return axios(getCinema + '?cinemaId=' + id)
}
// 影片排片档次
export function getCineAndFilm(ciId,fiId,data){
  return axios(getCinemaFilm+'?cinemaId='+ciId+'&filmId='+fiId+'&date='+data)
}

// 登录
export function login(data) {
  return axios(resLogin, {
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: {
      username: data.username,
      password: data.password
    }
  })
}
// 注册
export function Register(data) {
  return axios(resRegister, {
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: {
      username: data.username,
      password: data.password,
      email: data.email,
      tel: data.tel
    },
  })
}

